/*
 * FreeRTOS V202212.00
 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * https://www.FreeRTOS.org
 * https://github.com/FreeRTOS
 *
 */

/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "semphr.h"

/* Hardware and starter kit includes. */
#include "stm32f4xx.h"
#include "stm32f4xx_conf.h"
#include "uart_printf.h"
#include "elog.h"

#define LOG_TAG "MAIN"

void LEDTaskTest(void *argc)
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

    GPIO_InitTypeDef GPIOSturst;
    GPIOSturst.GPIO_Mode = GPIO_Mode_OUT;
    GPIOSturst.GPIO_OType = GPIO_OType_PP;
    GPIOSturst.GPIO_Pin = GPIO_Pin_13;
    GPIOSturst.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIOSturst.GPIO_Speed = GPIO_Fast_Speed;
    GPIO_Init(GPIOC, &GPIOSturst);

    while (1)
    {
        GPIO_ToggleBits(GPIOC, GPIO_Pin_13);
        vTaskDelay(500);
    }
}

void EasyLoggerTask(void *argc)
{
    // 等待初始化完成
    vTaskDelay(50);
    uint16_t i = 0;

    while (1)
    {
        log_d("Hello, EasyLogger! i = %d", i++);
        vTaskDelay(5);
    }
}

void EasyLoggerTask2(void *argc)
{
    // 等待初始化完成
    vTaskDelay(50);
    uint16_t i = 0;

    while (1)
    {
        log_e("Hello, EasyLogger! i = %d", i++);
        vTaskDelay(10);
    }
}

int main(void)
{
    /* Setup STM32 system (clock, PLL and Flash configuration) */
    SystemInit();

    /* Ensure all priority bits are assigned as preemption priority bits. */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

    /* SEGGER SystemView Config */
    SEGGER_SYSVIEW_Conf();

    USART_Printf_Init();

    xTaskCreate(elog_entry, "elog_entry", 1024, (void *)NULL, 7, NULL);
    xTaskCreate(LEDTaskTest, "LEDTaskTest", 256, (void *)NULL, 4, NULL);
    xTaskCreate(EasyLoggerTask, "EasyLoggerTask", 1024, (void *)NULL, 6, NULL);
    xTaskCreate(EasyLoggerTask2, "EasyLoggerTask2", 1024, (void *)NULL, 5, NULL);

    /* Start the scheduler. */
    vTaskStartScheduler();

    /* If all is well, the scheduler will now be running, and the following line
    will never be reached.  If the following line does execute, then there was
    insufficient FreeRTOS heap memory available for the idle and/or timer tasks
    to be created.  See the memory management section on the FreeRTOS web site
    for more details. */
    for (;;);

    return 0;
}
